package com.lanju.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanju.common.SpringUtils;
import com.lanju.entity.Community;
import com.lanju.entity.CommunityImg;
import com.lanju.model.SAModel;
import com.lanju.service.AreaCommunityService;
/**
 * 小区控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/community")
public class AreaCommunityController{
	
	@Autowired
	private AreaCommunityService areaCommunityService;
	
	@RequestMapping("/addAreaCommunity")
	@ResponseBody
	public Map<String,Object> addAreaCommunity(HttpServletRequest request,HttpSession httpSession,
			@RequestParam(value="files",required=false) MultipartFile[] files,
			@RequestParam(value="delStr",required=false) String delStr){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		SAModel systemAdminModel = (SAModel) httpSession.getAttribute("saModel");
		param.put("userConde", systemAdminModel.getAdminCode());
		String fileUrl=filesUpload(files,delStr);
		param.put("fileUrl", fileUrl);
		return  areaCommunityService.addAreaCommunity(param);
	}

		@RequestMapping("filesUpload")
		@ResponseBody
		public String filesUpload( MultipartFile[] files,  String delStr) {
			
			String fileUrl="";
			String fileTypeStr="";
			String result="success";
			// 判断file数组不能为空并且长度大于0
			if (files != null && files.length > 0) {
				// 循环获取file数组中得文件
				for (int i = 0; i < files.length; i++) {
					MultipartFile file = files[i];
					try {
						
						 OutputStream os = null;
						 InputStream in= null;
						 String picPath=null;
						in=file.getInputStream();
						 // 2、保存到临时文件
			            // 1K的数据缓冲
			            byte[] bs = new byte[1024];
			            // 读取到的数据长度
			            int len;
			            // 输出的文件流保存到本地文件
//			            File tempFile = new File("/webapp/lanjufile/commuinty");//linux
			            File tempFile = new File("D:/upFiles/commuinty");
			            if (!tempFile.exists()) {
			                tempFile.mkdirs();
			            }else{
//			            	LOGGER.info("文件已存在");
			            	System.out.println("文件已存在");
			            }
			            String fullPath="D:/upFiles/commuinty/"+file.getOriginalFilename();
//			            String fullPath="/webapp/lanjufile/commuinty/"+file.getOriginalFilename();//linux
			            String vmURL="/dataResourceImages/commuinty/"+file.getOriginalFilename();
			            System.out.println("文件全路径"+fullPath);
			            System.out.println("vmURL=="+vmURL);
			            fileUrl+=vmURL+",";
			            String[] fullPathArr=fullPath.split("\\.");
			            fileTypeStr+=fullPathArr[fullPathArr.length-1]+",";
			            os = new FileOutputStream(fullPath);
			            // 开始读取
			            while ((len = in.read(bs)) != -1) {
			                os.write(bs, 0, len);
			            } 
			            os.close();
		                in.close();
		                System.out.println("文件保存的路径是："+fileUrl);
		             // 保存文件
					} catch (IOException e) {
						 result="fail";
						e.printStackTrace();
					}
					
				}
				
			}else{
				
			}
			// 重定向
			return fileUrl;
		}
	  
	  @RequestMapping("getAreaCommunityDetail")
	  @ResponseBody
	  public Map<String,Object> getAreaCommunityDetail(@RequestParam(value="community_id") Integer community_id){
		  Map<String,Object> resultMap=new HashMap<String, Object>();
		  try {
			  Community c=areaCommunityService.getAreaCommunityDetail(community_id);
			  Set<CommunityImg> set=c.getCommunityimg();
			  resultMap.put("imgList", set);
			  resultMap.put("community", c);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	  }
	  
	  @RequestMapping("getAreaCommunityList")
	  @ResponseBody
	  public Map<String,Object> getAreaCommunityList(HttpServletRequest request){
		  Map<String, Object> param = SpringUtils.getParameterMap(request);
		  return areaCommunityService.getAreaCommunityList(param);
	  }
	  
	  
}
